<?php
class Model{
	private $table='';
	private $options=array(
		'where'=>'',
		'group'=>'',
		'having'=>'',
		'order'=>'',
		'limit'=>''
		);
	public function __construct($table)
	{
		$this->table=$table;//$this->table;访问私有属性table(默认为空字符串)，并给它赋值为$table;
		$this->connect();
	}
	/**
	 * 连接数据库
	 * @return [type] [description]
	 */
	public function connect()
	{
		@mysql_connect(C('DB_HOST'),C('DB_USER'),C('DB_PASSWD')) or halt(mysql_error());
		mysql_select_db(C('DB_NAME'));
		mysql_query('SET NAMES '.C('DB_CHARSET'));
	}

	public function count($field='*')
	{
		$sql='select count('.$field.') c from '.$this->table;
		$result=$this->query($sql);
		return empty($result)?0:current($result)['c'];
	}

	public function all()
	{
		$sql='select * from '.$this->table.
		$this->options['where'].
		$this->options['order'].
		$this->options['limit'];
		p($sql);
		return $this->query($sql);
	}

	/**
	 * where语句
	 * @param  [type] $sql [description]
	 * @return [type]      [description]
	 */
	public function where($sql)
	{
		$this->options['where']=' where '.$sql;
		return $this;
	}

	/**
	 * order语句
	 * @param  [type] $sql [description]
	 * @return [type]      [description]
	 */
	public function order($sql)
	{
		$this->options['order']=' order by '.$sql;
		return $this;
	}
	/**
	 * limit语句
	 * @param  [type] $sql [description]
	 * @return [type]      [description]
	 */
	public function limit($sql)
	{
		$this->options['limit']=' limit '.$sql;
		return $this;
	}
	/**
	 * insert 语句
	 * @param [type] $data [description]
	 */
	public function add($data)
	{
		if (is_array($data)) {
			$field=implode(',', array_keys($data));
			$values=implode("','", array_values($data));
			$sql='insert into '.$this->table.'('.$field.") values('".$values."')";
			return $this->exe($sql);
		}
	}
	/**
	 * 更新记录
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public function edit($data)
	{
		$sql='update '.$this->table.' set ';
		foreach ($data as $field => $value) {
			$sql.=$field."='".$value."',";
		}
		$sql=substr($sql, 0,-1);
		$sql.=$this->options['where'];
		return $this->exe($sql);
		// echo $sql;

	}

	public function del()
	{
		$sql='delete from '.$this->table.$this->options['where'];
		return $this->exe($sql);
	}

	public function error(){
		if(DEBUG){
			halt(mysql_error());
		}	
	}
	/**
	 * 执行sql查询
	 * @param  [type] $sql [description]
	 * @return [type]      [description]
	 */
	public function query($sql)
	{
		if ($result=mysql_query($sql)) {
			$arr=array();
			while ($d=mysql_fetch_assoc($result)) {
				$arr[]=$d;
			}
			return $arr;
		}else{
			return false;
		}
	}
	/**
	 * 把形如：
	 *Array
	 *(
	 *    [0] => Array
	 *        (
	 *            [sid] => 4
	 *            [sname] => 呀哈哈
	 *            [birthday] => 1911-03-17
	 *            [sex] => 男
	 *            [cid] => 2
	 *        )
	 *
	 *)的二维数组转换成形如：
	 *Array
	 *(
	 *    [sid] => 4
	 *    [sname] => 呀哈哈
	 *    [birthday] => 1911-03-17
	 *    [sex] => 男
	 *    [cid] => 2
	 *)的一维数组
	 *用在编辑一篇文件时，便于操作
	 * @return [type] [arr]
	 */
	public function find()
	{
		$result=$this->all();
		return $result?current($result):null;
	}

	public function exe($sql)
	{
		if (mysql_query($sql)) {
			return mysql_insert_id()?mysql_insert_id():mysql_affected_rows();
		}else {
			$this->error();
			return false;
		}
		
	}


}